package com.czmh.etc.onlinepubsys.controller;

import javax.servlet.http.HttpServletRequest;

import com.czmh.etc.onlinepubsys.json.JsonResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.czmh.etc.common.core.annotation.ApiVersion;
import com.czmh.etc.common.core.utils.UUIDGenerator;
import com.czmh.etc.onlinepubsys.json.DetailJson;
import com.czmh.etc.onlinepubsys.request.WxPrePayRequest;
import com.czmh.etc.onlinepubsys.response.WxPrePayResponse;
import com.czmh.etc.onlinepubsys.services.EtcOrderService;
import com.czmh.etc.onlinepubsys.utils.RequestUtil;
import com.czmh.etc.onlinepubsys.utils.ResponseJsonUtil;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * @ClassName：TransferController
 * @Description：TODO
 * @Author： sevenchen.
 * @Date：2019/6/14 01:59.
 * @Version：1.0.0
 */
@RestController
@RequestMapping("/{version}/wx")
@Api(tags = {"微信支付相关接口"})
public class TransferController {
    private Logger logger = LoggerFactory.getLogger(TransferController.class);

    @Autowired
    private EtcOrderService etcOrderService;

    @ApiVersion(1)
    @PostMapping(value = "/wechatpay/unifiedorder")
    @ApiOperation(value = "微信支付统一下单", notes = "微信支付统一下单")
    public DetailJson<WxPrePayResponse> wxPrePay(@RequestBody WxPrePayRequest wxPrePayRequest, HttpServletRequest request) throws Exception {
        String ip = RequestUtil.getRemoteIp(request);
        WxPrePayResponse data = etcOrderService.preWxPayOrder(wxPrePayRequest.getOrder_id(), wxPrePayRequest.getLoginUserId(), ip);
        return ResponseJsonUtil.getDetailJson(data);
    }
    
    @ApiVersion(1)
    @PostMapping(value = "/secapi/pay/refund")
    @ApiOperation(value = "申请退款", notes = "申请退款")
    public JsonResult refund(@RequestBody WxPrePayRequest wxPrePayRequest, HttpServletRequest request) throws Exception {
        return etcOrderService.refundWxPayOrder(wxPrePayRequest.getOrder_id(), wxPrePayRequest.getLoginUserId());
    }
}
